package com.midea.ai.air.ui.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.home.data.ParaValueModel;

import java.util.List;

/**
 * Created by ouyangdi on 2016/4/8.
 * 编辑房间->情景设置Adapter
 */
public class EditSceneSetAdapter extends ArrayAdapter<ParaValueModel>{
    private LayoutInflater mInflater;
    private boolean mIsSceneOn = false;

    public EditSceneSetAdapter(Context context, int resource, List<ParaValueModel> objects) {
        super(context, resource, objects);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ParaValueModel item = getItem(position);
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        int type = getItemViewType(position);

        if(convertView == null) {
            if(type == ParaValueModel.TYPE_VAL){
                convertView = mInflater.inflate(R.layout.item_scene_set_value,parent,false);
                holder1 = new ViewHolder1();
                holder1.tvPara = (TextView)convertView.findViewById(R.id.tv_scene_set_para);
                holder1.tvParaVal = (TextView)convertView.findViewById(R.id.tv_scene_set_val);
                holder1.botLine = convertView.findViewById(R.id.bot_line);
                convertView.setTag(holder1);

            } else {
                convertView = mInflater.inflate(R.layout.item_scene_set_switch,parent,false);
                holder2 = new ViewHolder2();
                holder2.tvPara = (TextView)convertView.findViewById(R.id.tv_scene_set_para);
                holder2.tvSwitch = (ImageView)convertView.findViewById(R.id.tv_scene_set_switch);
                holder2.tvSwitch.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageView view = (ImageView)v;
                        if(mIsSceneOn) {
                            view.setImageResource(R.drawable.more_setting_location_bt_off);
                            mIsSceneOn = false;
                            return;
                        }
                        view.setImageResource(R.drawable.more_setting_location_bt_on);
                        mIsSceneOn = true;
                    }
                });
                holder2.botLine = convertView.findViewById(R.id.bot_line);
                convertView.setTag(holder2);
            }
        }

        else {
            if(type == ParaValueModel.TYPE_VAL) {
                holder1 = (ViewHolder1) convertView.getTag();
            }
            else {
                holder2 = (ViewHolder2) convertView.getTag();
            }
        }

        if(type == ParaValueModel.TYPE_VAL) {

            holder1.tvPara.setText(item.getPara());
            holder1.tvParaVal.setText(item.getValue());
            if(position == getCount()-1) {
                holder1.botLine.setVisibility(View.INVISIBLE);
            } else {
                holder1.botLine.setVisibility(View.VISIBLE);
            }

        } else{
            holder2.tvPara.setText(item.getPara());
            if(position == getCount()-1) {
                holder2.botLine.setVisibility(View.INVISIBLE);
            } else {
                holder2.botLine.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getType();
    }

    private class ViewHolder1 {
        public TextView tvPara;
        public TextView tvParaVal;
        public View botLine;
    }

    private class ViewHolder2 {
        public TextView tvPara;
        public ImageView tvSwitch;
        public View botLine;
    }

    @Override
    public boolean isEnabled(int position) {
        if(getItemViewType(position) == ParaValueModel.TYPE_SWITCH) {
            return false;
        }
        return true;
    }
}
